﻿namespace DCHealthCheck.DomainModel {

    #region Usings
    using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks; 
    #endregion

    [Table("RoleFeature")]
    public class RoleFeature : IRoleFeature, IObjectWithState, IValidatableObject {

        #region Members
        public virtual int FeatureId { get; set; }
        [Key]
        public virtual long Id { get; set; }
        public virtual string Name { get; set; }
        public virtual int ParentFeatureId { get; set; }

        #region Related Entities
        public virtual SystemInfo SystemInfo {
            get { return systemInfo; }
            set {
                systemInfo = value;
                if (systemInfo != null) {
                    if (this.SystemInfoId != systemInfo.Id) {
                        this.SystemInfoId = systemInfo.Id;
                    }
                }
            }
        }
        private SystemInfo systemInfo;

        [ForeignKey("SystemInfo")]
        public virtual Guid SystemInfoId {
            get {return this.SystemInfo != null ? this.SystemInfo.Id : systemInfoId; }
            set { systemInfoId = value; }
        }
        private Guid systemInfoId;
        #endregion

        #region NotMapped properties
        [NotMapped]
        public DetachedState DetachedState { get; set; }

        [NotMapped]
        public Object EntityKey { get { return this.Id; } }

        [NotMapped]
        public Dictionary<string, object> StartingOriginalValues { get; set; }

        #endregion
        #endregion

        #region Constructor
        public RoleFeature() {

        } 
        #endregion

        #region Methods

        public override string ToString() {
            if (string.IsNullOrWhiteSpace(this.Name)) {
                return "N/A";
            }
            else {
                return string.Format("Name: {0} FeatureId: {1} ParentFeatureId: {2}", this.Name, this.FeatureId, this.ParentFeatureId);
            }
        }

        public IEnumerable<ValidationResult> Validate(ValidationContext validationContext) {
            if ((this.DetachedState != DetachedState.Added) && (this.Id < 1)) {
                yield return new ValidationResult(
                    "Id is required.", new[] { "Id" });
            }

            if (this.DetachedState != DetachedState.Deleted) {
                if (string.IsNullOrWhiteSpace(this.Name)) {
                    yield return new ValidationResult(
                        "Name is required.", new[] { "Name" });
                }

                if (this.FeatureId < 1) {
                    yield return new ValidationResult(
                        "FeatureId is required.", new[] { "FeatureId" });
                }
            } // if (this.DetachedState != State.Deleted)
        }  
        #endregion 

    }
}
